package com.incross.dawin.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;

import android.content.Context;
import android.os.AsyncTask;

import com.incross.dawin.util.BKLog;

public class BKDataAsyncTask extends AsyncTask<String, Void, HttpResponse>{
	
	public static final int HTTP_STATUS_OK = 200;
		
	private Context mContext;
	private int mHttpMethod = BKConnectionManager.HTTP_GET;
	private String mUrl;
	private int mCurrentRequest;
	private String mParsingString;
	
	private OnTaskStateListener mTaskListener;
	
	public interface OnTaskStateListener {
		public abstract void onTaskFinished(int request, HttpResponse response, String extractString);
		public abstract void onHttpRequestError(int request, int resultCode, HttpResponse response);
		
	}
	
	public void setOnTaskStateListener(OnTaskStateListener listener) {
		mTaskListener = listener;
	}
	
	public void setHttpMethod(int method) {
		mHttpMethod = method;
	}
	
	public void setRequestCode(int request) {
		mCurrentRequest = request;
	}
	
	public BKDataAsyncTask(Context context) {
		mContext = context;
	}
	
	public BKDataAsyncTask(Context context, OnTaskStateListener listener) {
		mContext = context;
		mTaskListener = listener;
	}
	
	@Override
	protected void onPreExecute() {
		BKLog.d("onPreExecute()");
	}

	@Override
	protected HttpResponse doInBackground(String... params) {
		BKLog.d("doin background");
		HttpResponse result = null;
		mUrl = params[0];
		if(mUrl != null && !mUrl.equals("")) {
			BKLog.i("url : " + mUrl);
			result = BKHttpRequest.getResponseForGet(mUrl, null, mContext);
			if(result != null) {
				mParsingString = getParsingString(result);
			}
			
			return result;
		} else {
			BKLog.e("url null or empty!!");
			return null;
		}
	}
	
	@Override
	protected void onPostExecute(HttpResponse response) {
		if(response != null) {
			int status = response.getStatusLine().getStatusCode();
			BKLog.i("Response code : " + status);
			if(status == HTTP_STATUS_OK) {
				if(mTaskListener != null) {
					mTaskListener.onTaskFinished(mCurrentRequest, response, mParsingString);
				}
			} else {
				if(mTaskListener != null) {
					mTaskListener.onHttpRequestError(mCurrentRequest, status, response);
				}
			}
		} else {
			if(mTaskListener != null) {
				mTaskListener.onHttpRequestError(mCurrentRequest, BKConnectionManager.OTHER_CONNECTION_ERROR, response);
			}
		}
		
	}
	
	public String getParsingString(HttpResponse response) {
		InputStream is = null;
		String xmlStringResult = "";
		
		try {
			is = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"), 8);
			
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			xmlStringResult = sb.toString();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return xmlStringResult;
	}

}
